package com.edu.spider;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.edu.spider.DTO.SearchCTripTrainDTO;
import com.edu.spider.enity.TrainItem;
import com.edu.spider.util.JSONToBeanUtil;


/** 
 * @author : ljl
 * @date 创建时间：2018年9月19日 上午9:52:54 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */

public class Test {

	@SuppressWarnings("unchecked")
	public void ClimbTicketInformation(){
		RestTemplate restTemplate = new RestTemplate();
		String url = "http://trains.ctrip.com/TrainBooking/Ajax/SearchListHandler.ashx?Action=getSearchList";
		SearchCTripTrainDTO searchTrainDTO = new SearchCTripTrainDTO();
		searchTrainDTO.setIsBus(false);
		searchTrainDTO.setFilter("0");
		searchTrainDTO.setIsGaoTie(false);
		searchTrainDTO.setIsDongChe(false);
		searchTrainDTO.setDepartureCity("hangzhou");
		searchTrainDTO.setDepartureCityName("杭州");
		searchTrainDTO.setArrivalCity("shanghai");
		searchTrainDTO.setArrivalCityName("上海");
		searchTrainDTO.setDepartureDate("2018-09-20");
		JSONObject json2 = JSONObject.fromObject(searchTrainDTO);
		MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
		postParameters.add("value", JSONObject.fromObject(searchTrainDTO).toString());
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(postParameters, headers);		
		ResponseEntity<String> document = restTemplate.postForEntity(url, httpEntity, String.class);
		JSONObject json = JSONObject.fromObject(document.getBody());
		List<TrainItem> TrainItems = (List<TrainItem>) json.get("TrainItemsList");
		TrainItem trainItem = JSONToBeanUtil.jsonToBean(String.valueOf(TrainItems.get(0)), TrainItem.class);
//		TrainItem TrainItem = TrainItems.get(0);
		System.out.println(trainItem);
		for (TrainItem trainItem2 : TrainItems) {
			System.out.println(trainItem2);
		}
		System.out.println(TrainItems);			
	}
	
	public static void main(String[] args) throws ParseException {
//		new Test().ClimbTicketInformation();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
		Long departureDate = sdf.parse("2018-09-18 23:59:59").getTime();
		Long nowDate = new Date().getTime();
		int maxTimes = Math.round((departureDate-nowDate)/(30 * 60 * 1000));
		System.out.println(maxTimes);
	}
	
}
